//
//  FCRelationBindRequestViewController.m
//  customerRelation_Example
//
//  Created by 陈文斌 on 2019/2/20.
//  Copyright © 2019 64912907. All rights reserved.
//

#import "FCRelationBindRequestViewController.h"
#import "FCBtnTypeRuleView.h"
#import "FCRelationBindCompanyInfoTableViewCell.h"
#import "FCRelationBindEmployeeListTableViewCell.h"
#import "FCRelationBindInfoSelectedTableViewCell.h"
#import "CustomerRelationHeader.h"
#import "UIImage+imageExtension.h"
#import "UIImageView+WebCache.h"
#import "FCCustomerRelationRequest.h"
#import "FCRelationTyleSheetViewController.h"
#import "FCCompanyCardTool.h"
#import "UIViewController+runtimeCheck.h"
#import "MJExtension.h"
#import "FCBaseTool.h"
#import "FCCommonRequest.h"

@interface FCRelationBindRequestViewController ()<btnTypeRuleDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *relationTypeIdArr;
@property (nonatomic, strong) NSMutableArray *relationEmployeeIdArr;

@end

@implementation FCRelationBindRequestViewController
- (NSMutableArray *)relationTypeIdArr{
    if (!_relationTypeIdArr) {
        _relationTypeIdArr = [NSMutableArray array];
    }
    return _relationTypeIdArr;
}
- (NSMutableArray *)relationEmployeeIdArr{
    if (!_relationEmployeeIdArr) {
        _relationEmployeeIdArr = [NSMutableArray array];
    }
    return _relationEmployeeIdArr;
}

- (void)setupUI{
    WJCWeakSelf
    self.title = customerRelationLocaChanged(@"关系绑定请求", @"关系绑定请求");
    self.view.backgroundColor = UIColor.whiteColor;
    
    FCBtnTypeRuleView *bottomReviewView = [[FCBtnTypeRuleView alloc] initWithFrame:CGRectMake(0, WJCScreenH - 80 - 64 - FCTabbarSafeBottomMargin, WJCScreenW, 80)];
    [self.view addSubview:bottomReviewView];
    bottomReviewView.backgroundColor = [UIColor whiteColor];
    bottomReviewView.ruleType = ruleAverageRadius;
    bottomReviewView.btnArr = [NSMutableArray arrayWithObjects:[btnTypeModel itemWithTitle:customerRelationLocaChanged(@"驳回请求", @"驳回请求") btnImage:nil detail:nil btnType:btnNomal itemType:0], [btnTypeModel itemWithTitle:customerRelationLocaChanged(@"通过请求", @"通过请求") btnImage:nil detail:nil btnType:btnNomal itemType:1], nil];
    bottomReviewView.delegate = self;
//    BA_ViewBorderRadius(bottomReviewView, 0, 1, FCLineColor);
    for (UIButton *subBtn in bottomReviewView.subviews) {
        subBtn.y = 12;
        subBtn.height = 42;
        if ([subBtn.titleLabel.text isEqualToString:customerRelationLocaChanged(@"驳回请求", @"驳回请求")]) {
            BA_ViewBorderRadius(subBtn, 2, 1, FCLineColor);
            [subBtn setTitleColor:FCRedBackColor forState:UIControlStateNormal];
        } else{
            subBtn.backgroundColor = FCBlueBackColor;
            BA_ViewBorderRadius(subBtn, 2, 0, FCLineColor);
            [subBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    if (self.relation.statusInt == 2) {
        bottomReviewView.height = 0.01f;
    }
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, bottomReviewView.width, 0.5)];
    lineView.backgroundColor =FCLineColor;
    [bottomReviewView addSubview:lineView];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
//    self.tableView.backgroundView = FCBackColor;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.right.mas_equalTo(0);
        if (weakSelf.relation.statusInt == 2) {
            make.bottom.mas_equalTo(0);
        } else{
            make.bottom.equalTo(bottomReviewView.mas_top).mas_equalTo(0);
        }
    }];
    [self setupTableView];
}

-(void)btnTypeRuleClickWithBtn:(UIButton*)sender andView:(FCBtnTypeRuleView*)view{
//    if (![FCEmployeeModel isBusinessWithNotice:YES andPushController:self]) {
//        return;
//    }
    if ([sender.titleLabel.text isEqualToString:customerRelationLocaChanged(@"驳回请求", @"驳回请求")]) {
        XMGLog(@"驳回请求");
        NSDictionary *args = @{
                               @"id": self.relation.Id,
                               @"agree": @(NO),
                               };
//        [SVProgressHUD showWithStatus:customerRelationLocaChanged(@"正在处理关系", @"正在处理关系")];
        WJCWeakSelf
        [FCCustomerRelationRequest dealRelationRequestWithArgs:args vc:self success:^(id resultObj) {
            [SVProgressHUD showSuccessWithStatus:customerRelationLocaChanged(@"employee_equest_handle_success", @"处理成功")];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } failure:^(NSError *error) {
            XMGLog(@"error ==> %@", error);
//            [SVProgressHUD dismiss];
        }];
    }
    if ([sender.titleLabel.text isEqualToString:customerRelationLocaChanged(@"通过请求", @"通过请求")]) {
        XMGLog(@"通过请求");
        if (self.relationTypeIdArr.count == 0) {
            [SVProgressHUD showInfoWithStatus:customerRelationLocaChanged(@"请选择上下游关系", @"请选择上下游关系")];
            return;
        }
        if (self.relationEmployeeIdArr.count == 0) {
            [SVProgressHUD showInfoWithStatus:customerRelationLocaChanged(@"请选择指派企业联系人", @"请选择指派企业联系人") ];
            return;
        }
        NSDictionary *args = @{
                               @"id": self.relation.Id,
                               @"agree": @(YES),
                               @"addEmpIds": self.relationEmployeeIdArr,
                               @"addTypeIds": self.relationTypeIdArr
                               };
        [SVProgressHUD showWithStatus:customerRelationLocaChanged(@"正在处理关系", @"正在处理关系")];
        WJCWeakSelf
        [FCCustomerRelationRequest dealRelationRequestWithArgs:args vc:self success:^(id resultObj) {
            [SVProgressHUD showSuccessWithStatus:customerRelationLocaChanged(@"employee_equest_handle_success", @"处理成功")];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } failure:^(NSError *error) {
            [SVProgressHUD dismiss];
        }];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

#pragma mark - tableView
- (void)setupTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = FCBackColor;
    self.tableView.estimatedRowHeight = 44;
    self.tableView.sectionHeaderHeight = 12;
    self.tableView.sectionFooterHeight = 0;
    [self setExtraCellLineHidden:self.tableView];
    self.tableView.separatorStyle = UITableViewCellEditingStyleNone;
    
    WJCWeakSelf
    if (!self.relation.me) {
        [FCCommonRequest companyInfoWithCompanyId:self.relation.meId success:^(FCCompanyModel * _Nonnull company) {
            weakSelf.relation.me = company;
            [self.tableView reloadData];
        } failure:^(NSError * _Nonnull error) {
            
        }];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = FCBackColor;
    
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        FCRelationBindCompanyInfoTableViewCell *cell = [[FCRelationBindCompanyInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"companyInfo"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        if (self.relation.me.LogoUrl.length) {
            [cell.avatarImageView sd_setImageWithURL:[NSURL URLWithString:self.relation.me.LogoUrl] placeholderImage:LogoPlaceImage];
        } else{
//            if ([relationType.code isEqualToString:@"processor"]) {
//                // 加工商
//                cell.logoImageLabel.backgroundColor = FCRedBackColor;
//                cell.logoImageLabel.alpha = 0.6;
//            } else if([relationType.code isEqualToString:@"supply"]){
//                // 供应商
//                cell.logoImageLabel.backgroundColor = FCOrangeColor;
//                cell.logoImageLabel.alpha = 0.6;
//            } else if([relationType.code isEqualToString:@"purchase"]){
//                // 供应商
//                cell.logoImageLabel.backgroundColor = FCBlueTxtColor;
//                cell.logoImageLabel.alpha = 0.6;
//            } else if([relationType.code isEqualToString:@"server"]){
//                // 供应商
//                cell.logoImageLabel.backgroundColor = HEXCOLOR(0x1FA679);
//                cell.logoImageLabel.alpha = 0.6;
//            } else{
                // 其它
                cell.logoImageLabel.backgroundColor = HEXCOLOR(0xB87333);
                cell.logoImageLabel.alpha = 0.4;
//            }
            if (self.relation.me.BrandName.length > 2) {
                NSString *logoStr = [self.relation.me.BrandName substringToIndex:2];
                cell.logoImageLabel.text = logoStr;
            } else{
                cell.logoImageLabel.text = self.relation.me.BrandName;
            }
        }
        
            cell.companyNameLabel.text = self.relation.me.BrandName ?: @"";
            if (self.relation.me.CustomMainBusiness.length > 0) {
                cell.mainBusinessLabel.text = [NSString stringWithFormat:@"%@%@", customerRelationLocaChanged(@"主营：", @"主营："),self.relation.me.CustomMainBusiness];
            } else{
                cell.mainBusinessLabel.text = @"主营：老板生意很好，还没添加主营信息";
            }
            cell.addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@%@",self.relation.me.ResidentCountry ?: @"",self.relation.me.ResidentProvince ?: @"",self.relation.me.ResidentCity ?: @"",self.relation.me.ResidentArea ?: @"",self.relation.me.Address ?: @""];
            
            UIView *telView = [FCCompanyCardTool telPhoneWithCompanyModelOwnerIdentityModel:self.relation.me];
            [cell.phoneView addSubview:telView];
            [telView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.and.top.and.bottom.mas_equalTo(0);
                make.width.mas_equalTo(telView.width);
            }];
        
        return cell;
    }
    if (indexPath.section == 1) {
        FCRelationBindEmployeeListTableViewCell *cell = [[FCRelationBindEmployeeListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"employeeList" employees:self.relation.emps];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    if (indexPath.section == 2) {
        FCRelationBindInfoSelectedTableViewCell *cell = [[FCRelationBindInfoSelectedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"infoSelect"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        WJCWeakSelf
        cell.relationTypeIdsBlock = ^(NSMutableArray * _Nonnull relationTypeIds) {
            weakSelf.relationTypeIdArr = relationTypeIds;
        };
        cell.employeeIdsBlock = ^(NSMutableArray * _Nonnull employeeIds) {
            weakSelf.relationEmployeeIdArr = employeeIds;
        };
        
        return cell;
    }
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    [tableView setTableFooterView:view];
}

@end
